package com.hotel.action;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hotel.dto.Envelope;
import com.hotel.service.AdminService;

@Controller
@RequestMapping("/system")
public class SystemAction {
	private AdminService adminService;
	public SystemAction() {
		this.adminService=new AdminService();
	}
	@RequestMapping("/error")
	public String error(){
		return "system/error";
	}
	@RequestMapping("/loginPage")
	public String loginPage(){
		return "system/loginPage";
	}
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	@ResponseBody
	public String login(String username,String password,HttpSession session){
		//System.out.println(username);
		try{
			Envelope env= adminService.login(username, password);
			if(env.getCode()==0){
				session.setAttribute("user", env.getObj());
				//AdminsEntity entity=(AdminsEntity) env.getObj();
				env.setObj(null);
			}
			return env.toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping("/mainPage")
	public ModelAndView mainPage(HttpSession session){
		ModelAndView model=new ModelAndView("system/mainPage");
		//AdminsEntity entity=(AdminsEntity) session.getAttribute("user");
		//model.addObject("username",entity.getUsername());
		return model;
	}
}
